package com.ssdp.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ssdp.constant.PageConstant;
import com.ssdp.dto.Result;
import com.ssdp.entity.Shop;
import com.ssdp.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @author syo 2319615164@qq.com
 * @description 店铺接口
 * @date 2024/3/14
 * @Copyright syo
 */
@Api(tags = "店铺相关接口")
@RestController
@RequestMapping("/shop")
@RequiredArgsConstructor
public class ShopController {

    public final IShopService shopService;

    @ApiOperation("根据店铺Id查询店铺信息")
    @GetMapping("/{id}")
    public Result queryShopById(@PathVariable("id") Long id) throws JsonProcessingException {
        return shopService.queryShopById(id);
    }

    @ApiOperation("新增店铺")
    @PostMapping
    public Result saveShop(@RequestBody Shop shop) {
        shopService.save(shop);
        return Result.ok(shop.getId());
    }

    @ApiOperation("更新店铺信息")
    @PutMapping
    public Result updateShop(@RequestBody Shop shop) {
        return shopService.updateShop(shop);
    }


    @ApiOperation("根据店铺类型分页查询店铺信息")
    @GetMapping("/of/type")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x", required = false) Double x,
            @RequestParam(value = "y", required = false) Double y
    ) {

        return shopService.queryShopByType(typeId, current, x, y);
    }

    @ApiOperation("根据店铺名称查询店铺信息")
    @GetMapping("/of/name")
    public Result queryShopByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Shop> page = shopService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, PageConstant.MAX_PAGE_SIZE));
        // 返回数据
        return Result.ok(page.getRecords());
    }
}
